package com.ruoyi.datam.controller;

import com.ruoyi.common.core.domain.base.datam.VersionUpgrade;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.datam.service.IVersionUpgradeService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * App版本号Controller
 *
 * @author hwd
 * @date 2023-05-18
 */
@RestController
@RequestMapping("/VersionUpgrade")
public class VersionUpgradeController extends BaseController
{
    @Autowired
    private IVersionUpgradeService versionUpgradeService;

    /**
     * 查询App版本号列表
     */
    @RequiresPermissions("datam:VersionUpgrade:list")
    @GetMapping("/list")
    public TableDataInfo list(VersionUpgrade versionUpgrade)
    {
        startPage();
        List<VersionUpgrade> list = versionUpgradeService.selectVersionUpgradeList(versionUpgrade);
        return getDataTable(list);
    }

    /**
     * 导出App版本号列表
     */
    @RequiresPermissions("datam:VersionUpgrade:export")
    @Log(title = "App版本号", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VersionUpgrade versionUpgrade)
    {
        List<VersionUpgrade> list = versionUpgradeService.selectVersionUpgradeList(versionUpgrade);
        ExcelUtil<VersionUpgrade> util = new ExcelUtil<VersionUpgrade>(VersionUpgrade.class);
        util.exportExcel(response, list, "App版本号数据");
    }

    /**
     * 获取App版本号详细信息
     */
    @RequiresPermissions("datam:VersionUpgrade:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(versionUpgradeService.selectVersionUpgradeById(id));
    }

    /**
     * 获取App版本号详细信息
     */
    @RequiresPermissions("datam:VersionUpgrade:query")
    @GetMapping(value = "/newestApp")
    public AjaxResult newestApp()
    {
        return success(versionUpgradeService.selectVersionUpgradeNewestApp());
    }

    /**
     * 新增App版本号
     */
    @RequiresPermissions("datam:VersionUpgrade:add")
    @Log(title = "App版本号", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VersionUpgrade versionUpgrade)
    {
        if (versionUpgrade.getApkUrl()!=null){
            // 获取当前的用户
            LoginUser loginUser = SecurityUtils.getLoginUser();
            if (StringUtils.isNotNull(loginUser)) {
                SysUser currentUser = loginUser.getSysUser();
                versionUpgrade.setCreatePerson(currentUser.getUserName());
                versionUpgrade.setCreateTime(new Date());
                versionUpgrade.setUpdatePerson(currentUser.getUserName());
                versionUpgrade.setUpdateTime(new Date());
            }else {
                return toAjax(0);
            }
        }
        return toAjax(versionUpgradeService.insertVersionUpgrade(versionUpgrade));
    }

    /**
     * 修改App版本号
     */
    @RequiresPermissions("datam:VersionUpgrade:edit")
    @Log(title = "App版本号", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VersionUpgrade versionUpgrade)
    {
        if (versionUpgrade.getApkUrl()!=null){
            // 获取当前的用户
            LoginUser loginUser = SecurityUtils.getLoginUser();
            if (StringUtils.isNotNull(loginUser)) {
                SysUser currentUser = loginUser.getSysUser();
                versionUpgrade.setUpdatePerson(currentUser.getUserName());
                versionUpgrade.setUpdateTime(new Date());
            }else {
                return toAjax(0);
            }
        }
        return toAjax(versionUpgradeService.updateVersionUpgrade(versionUpgrade));
    }

    /**
     * 删除App版本号
     */
    @RequiresPermissions("datam:VersionUpgrade:remove")
    @Log(title = "App版本号", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(versionUpgradeService.deleteVersionUpgradeByIds(ids));
    }
}
